package com.wytec.xywzxw.controller;

import com.wytec.xywzxw.bean.User;
import com.wytec.xywzxw.bean.commonBean.CommonResult;
import com.wytec.xywzxw.bean.commonBean.PageSizeBean;
import com.wytec.xywzxw.configuration.MyException;
import com.wytec.xywzxw.service.UserService;
import com.wytec.xywzxw.utils.CommonUtils;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "用户模块")
public class UserController extends BaseApiController{
    @Autowired
    UserService userService;

    @GetMapping("/user/login")
    @ApiOperation("用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名"),
            @ApiImplicitParam(name = "password", value = "密码"),
    })
    private CommonResult<Object> login(@RequestParam(name = "username") String username,
                                       @RequestParam(name = "password") String password) {

        try {
            User user = userService.userLogin(username, password);
            return CommonResult.success(user);
        } catch (MyException e) {
            return CommonResult.failed(e.getMessage());
        }
    }
    @GetMapping("/user/list")
    @ApiOperation("用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数"),
            @ApiImplicitParam(name = "size", value = "数据量"),
            @ApiImplicitParam(name = "keyword", value = "用户昵称/手机号"),
    })
    private CommonResult<Object> list(PageSizeBean page,String keyword) {
            List<User> userList = userService.getUserList(page, keyword);
            return CommonResult.success(CommonUtils.createPageDataMap(userList));
    }
    @PostMapping("/user/regisit")
    @ApiOperation("用户注册")
    private CommonResult<Object> regisit(@RequestBody User user) {
        try {
            user = userService.userSignIn(user);
            return CommonResult.success(user);
        } catch (MyException e) {
            return CommonResult.failed(e.getMessage());
        }
    }

}
